require "Common/define"
require "Logic/Config"
require "Common/functions"
local AddMessagePanel = require "View/UIFriend/UIFriendAddMessagePanel"
local UIFriendShowInfo = require "View/UIFriend/UIFriendShowInfo"

UIFriend = {};
HelperFunc.SetUIEnv(UIFriend)
local this = UIFriend;
this.hasInit = false;
this.btnFriendGift = nil
this.btnMyFriends = nil --头部页签
this.btnFriendApply = nil --头部页签 好友申请
this.btnFriendAdd = nil --头部页签 添加好友
this.btnBack = nil
this.btnDelFriBack = nil --插销删除好友
this.btnDelFri = nil --删除好欧
this.btnLoveGetAll = nil --一键领取爱心
this.btnAllDisAgree = nil --一键拒接
this.btnAllAgree = nil --一键同意
this.btnFreash = nil --刷新
this.btnAllJoin = nil --一键添加
this.btnFind = nil;
this.BG = nil;
this.txtTitle = nil;
this.txtLovePoint = nil;
this.todayget = nil;
this.dynamicList = nil;
this.dynamicList_Add = nil;
this.dynamicList_Apply = nil;
this.dynamicList_Del = nil;
this.inputField = nil;
this.isOpenShow = false;
this.State = {
    friends = 1,
    add = 2,
    apply = 3,
    adventure = 4,
}
this.curState = State.friends;
---@type GController = nil
this.friendController = nil;
this.isDelModel = false;
this.max_Friend = dataTable.getGlobalInt(196);
this.max_Love = 30;
this.tablist = nil
this.adviceFriend = nil
this.delFriBack = nil
this.delFri = nil
this.allAgree = nil
this.fresh = nil
this.loveGet = nil
this.allJoin = nil
this.InputTip = nil
this.disAgree = nil

local m_blackInit = false
local m_blackList = nil

function UIFriend.Init()
    local name, packageName = getUIName(PanelNames.UIFriend);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end
---@param obj GComponent
function UIFriend.onCreate(obj)
    if not hasInit then
        adviceFriend = obj:GetChild("adviceFriendText")
        delFriBack = obj:GetChild("btnDelFriBack"):GetChild("title")
        delFri = obj:GetChild("btnDelFri"):GetChild("title")
        allAgree = obj:GetChild("btnAllAgree"):GetChild("title")
        fresh = obj:GetChild("btnFreash"):GetChild("title")
        loveGet = obj:GetChild("btnLoveGetAll"):GetChild("title")
        disAgree = obj:GetChild("btnAllDisAgree"):GetChild("title")
        allJoin = obj:GetChild("btnAllJoin"):GetChild("title")
        InputTip = obj:GetChild("InputField")

        btnMyFriends = obj:GetChild("btnMyFriends")
        btnFriendApply = obj:GetChild("btnFriendApply")
        btnFriendAdd = obj:GetChild("btnFriendAdd")
        btnFriendGift = obj:GetChild("btnFriendGift")
        friendController = obj:GetController("friend")
        friendController.onChanged:Add(this.FriendPageChanged)
        this.ctrlResultOfSearch = obj:GetController("resultOfSearch")
        this.ctrlResultOfSearch.selectedIndex = 0

        btnBack = obj:GetChild("btnBack");
        btnbgClose = obj:GetChild("bgClose");
        btnDelFri = obj:GetChild("btnDelFri");
        btnDelFriBack = obj:GetChild("btnDelFriBack");
        btnLoveGetAll = obj:GetChild("btnLoveGetAll");
        btnAllDisAgree = obj:GetChild("btnAllDisAgree");
        btnAllAgree = obj:GetChild("btnAllAgree");
        btnFreash = obj:GetChild("btnFreash");
        btnRecord = obj:GetChild("btnRecord");
        btnAllJoin = obj:GetChild("btnAllJoin");
        txtLovePoint = obj:GetChild("txtLovePoint")
        this.totalLovePoint = obj:GetChild("title")
        todayget = obj:GetChild("todayget")

        txtTitle = obj:GetChild("numfriend")
        txtFriendNum = obj:GetChild("txtFriendNum")
        dynamicList = obj:GetChild("dynamicList")

        dynamicList_Apply = obj:GetChild("dynamicList_Apply")
        dynamicList_Add = obj:GetChild("dynamicList_Add")
        dynamicList_Del = obj:GetChild("dynamicList_Del")
        btnFind = obj:GetChild("btn_Find");
        inputField = obj:GetChild("InputField")
        this.findFriendItem = obj:GetChild("finded")
        this.ctrlNoFriend = obj:GetController("nofriend")
        ---@type UIFriendAddMessagePanel
        this.addMessagePanel = AddMessagePanel.New(obj:GetChild("applyMsg"))
        ---@type UIFriendShowInfo
        this.showDetailInfo = UIFriendShowInfo.New(obj:GetChild("checkInfo"))
        this.ctrlDetail = obj:GetController("query")
        this.ctrlAddMessage = obj:GetController("pnlMessage")
        this.recommendNetItemList = {}
        ctr_blackList = obj:GetController("blackList")
        ctr_blackList.selectedIndex = logicMgr.FriendManager.BlackIsEmpty() and 0 or 1

        this.scroll_black = obj:GetChild("list")
        this.scroll_black.itemRenderer = this.blackItemRender
        this.scroll_black:SetVirtual()
        
        for i = 0, 8 do
            local item = obj:GetChild("supPlayer" .. i)
            local data = {}
            data.obj = item
            data.head = item:GetChild("Head"):GetChild("Icon")
            data.name = item:GetChild("Name")
            data.title = item:GetChild("title")
            table.insert(this.recommendNetItemList, data)
        end
        this.addEvent();
    end
    friendController.selectedIndex = 0
    this.ctrlAddMessage.selectedIndex = 0
    this.initUI();
    isDelModel = false;
    this.showFriend(true);

    UIRedDots.addViewListen(PanelNames.UIFriend, "UIFriend" .. btnFriendApply.name, btnFriendApply, Config.RedDotsType.FriendApply);
    UIRedDots.addViewListen(PanelNames.UIFriend, "UIFriend" .. btnMyFriends.name, btnMyFriends, Config.RedDotsType.MyFriend);
    UIRedDots.addViewListen(PanelNames.UIFriend, "UIFriend" .. btnFriendGift .name, btnFriendGift, Config.RedDotsType.FriendGift);
    UIRedDots.addViewListen(PanelNames.UIFriend, "UIFriend" .. btnLoveGetAll .name, btnLoveGetAll, Config.RedDotsType.FriendLove);
    friendHandler.ReqFriendData()

end

function UIFriend.addEvent()
    btnMyFriends.onClick:Add(this.showFriend);
    btnFriendApply.onClick:Add(this.showFriendApply);
    btnFriendAdd.onClick:Add(this.showFriendAdd);
    btnFriendGift.onClick:Add(this.ShowFriendGift)
    btnBack.onClick:Add(this.onClickBack);
    btnbgClose.onClick:Add(this.onClickBack);
    btnDelFri.onClick:Add(this.onClickDelFri);
    btnDelFriBack.onClick:Add(this.onClickDelFriBack);
    btnLoveGetAll.onClick:Add(this.onClickLoveGetAll);
    btnAllDisAgree.onClick:Add(this.onClickAllRefuse);
    btnAllAgree.onClick:Add(this.onClickAllAgree);
    btnFreash.onClick:Add(this.onClickFreash);
    btnAllJoin.onClick:Add(this.onClickAllJoin);
    btnRecord.onClick:Add(this.OpenFriendCompeteBattleRecord);
    btnFind.onClick:Add(this.onClickFind);

end

function UIFriend.removeEvent()
    btnMyFriends.onClick:Remove(this.showFriend);
    btnFriendApply.onClick:Remove(this.showFriendApply);
    btnFriendAdd.onClick:Remove(this.showFriendAdd);
    btnFriendGift.onClick:Clear()
    btnBack.onClick:Remove(this.onClickBack);
    btnbgClose.onClick:Remove(this.onClickBack);
    btnDelFri.onClick:Remove(this.onClickDelFri);
    btnDelFriBack.onClick:Remove(this.onClickDelFriBack);
    btnLoveGetAll.onClick:Remove(this.onClickLoveGetAll);
    btnAllDisAgree.onClick:Remove(this.onClickAllRefuse);
    btnAllAgree.onClick:Remove(this.onClickAllAgree);
    btnFreash.onClick:Remove(this.onClickFreash);
    btnAllJoin.onClick:Remove(this.onClickAllJoin);
    btnFind.onClick:Remove(this.onClickFind);
    btnRecord.onClick:Remove(this.OpenFriendCompeteBattleRecord);

    friendController.onChanged:Clear()
    for k, v in pairs(this.recommendNetItemList) do
        v.obj.onClick:Clear()
    end
    UIRedDots.removeViewEvent(PanelNames.UIFriend)
end

function UIFriend.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_MyFriends, this.updateMyFriend);
    ListenEvent(Config.EventType.Fresh_ApplyFriend, this.updateApplyFriend);
    ListenEvent(Config.EventType.Fresh_RecommendFriend, this.updateRecommend);
    ListenEvent(Config.EventType.Fresh_Currency, this.updateLove);
    ListenEvent(Config.EventType.FreashRoleInfoOther, this.FreshRoleInfoOtherMsg)
    ListenEvent(Config.EventType.FriendShowAddMessage, this.ShowAddMessage)
    ListenEvent(Config.EventType.FriendShowDetail, this.ShowDetail)
    ListenEvent(Config.EventType.FriendCompeteBattle, this.OpenFriendCompeteBattle)
    ListenEvent(Config.EventType.Black_list_refresh, this.OnBlackListRefresh)

end

function UIFriend.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_MyFriends, this.updateMyFriend);
    CloseEvent(Config.EventType.Fresh_ApplyFriend, this.updateApplyFriend);
    CloseEvent(Config.EventType.Fresh_RecommendFriend, this.updateRecommend);
    CloseEvent(Config.EventType.Fresh_Currency, this.updateLove);
    CloseEvent(Config.EventType.FreashRoleInfoOther, this.FreshRoleInfoOtherMsg)
    CloseEvent(Config.EventType.FriendShowAddMessage, this.ShowAddMessage)
    CloseEvent(Config.EventType.FriendShowDetail, this.ShowDetail)
    CloseEvent(Config.EventType.FriendCompeteBattle, this.OpenFriendCompeteBattle)
    CloseEvent(Config.EventType.Black_list_refresh, this.OnBlackListRefresh)

    UIRedDots.removeViewEvent(PanelNames.UIFriend);
end

function UIFriend.OnDestroy()
    --this.removeEvent();
    this.addMessagePanel:Destroy()

end

function UIFriend.OnEnable()
    this.LocalListenEvent();
end

function UIFriend.OnDisable()
    this.LocalCloseEvent();
end

function UIFriend.initUI()
    txtTitle.text = getLanguage("Sys_Friend_Num") .. "(" .. "0/" .. max_Friend .. ")";
    txtFriendNum.text = ""
    inputField.text = "";
    adviceFriend.text = getLanguage("Sys_Friend_Recommend");
    delFriBack.text = getLanguage("Sys_FriendBtn_QuitDelete");
    delFri.text = getLanguage("Sys_FriendBtn_DeleteFriend");
    allAgree.text = getLanguage("Sys_FriendBtn_AgreeAll");
    fresh.text = getLanguage("Sys_Friend_Refresh");
    loveGet.text = getLanguage("Sys_FriendBtn_SendCollect");
    disAgree.text = getLanguage("Sys_FriendBtn_RefuseAll");
    allJoin.text = getLanguage("Sys_FriendBtn_AddAll");
    InputTip.promptText = getLanguage("Sys_Friend_SearchDes");

end

function UIFriend.CheckShowBtnState()
    local friendNum = this.GetFriendCount()
    local isShow = (friendNum > 0)
    btnLoveGetAll.visible = isShow
    btnDelFri.visible = isShow
    txtLovePoint.visible = isShow
    todayget.visible = isShow;
end

function UIFriend.GetFriendCount()
    local friendLsit = logicMgr.FriendManager.GetMyFriendList()
    return table.nums(friendLsit)
end

function UIFriend.initBtn()

end

--显示好友
function UIFriend.showFriend()
    curState = State.friends;
    isDelModel = false;
    this.updateMyFriend(true);
    this.CheckShowBtnState();
end

--显示好友申请
function UIFriend.showFriendApply()
    curState = State.apply;
    this.initBtn();
    this.updateApplyFriend();
    this.isRecommendSecond = false
end

--添加好友
function UIFriend.showFriendAdd()
    curState = State.add;
    this.initBtn();
    this.ctrlResultOfSearch.selectedIndex = 0
    inputField.text = "";
    local reCommandCount = table.getn(logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.recommend))
    if reCommandCount == 0 then
        friendHandler.getFriendRecommend();
    end
    this.updateRecommend();
end
function UIFriend.ShowFriendGift()
    showUI(PanelNames.UIFriendGift)
end

function UIFriend.setData(index, go)
    local itemData = tablist[index + 1];
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.FriendInfoItem);
    if ctrlItem ~= nil then
        --初始装备信息
        ctrlItem.initData(go, itemData);
    end
    if curState == this.State.apply then
        local msg = itemData.leaveMsg
        if string.IsNilOrEmpty(msg) then
            msg = string.format(getLanguage("UIFriend_ApplyMessage"), itemData.name)
        end
        ctrlItem.ShowMessage(go, msg)
    end
end

--返回
function UIFriend.onClickBack()
    closeUI(PanelNames.UIFriend)
    closeUI(PanelNames.UIFriendGift)
    if isOpenUI(PanelNames.UIFuncOpenView) then
        closeUI(PanelNames.UIFuncOpenView);
    end

        this.removeEvent();
end

--删除好友
function UIFriend.onClickDelFri()
    friendController.selectedIndex = 3
    isDelModel = true;
    this.updateMyFriend(true);
end

--退出删除好友
function UIFriend.onClickDelFriBack()
    friendController.selectedIndex = 0
    isDelModel = false;
    this.updateMyFriend(true);
end
--一件赠领
function UIFriend.onClickLoveGetAll()
    --[[if logicMgr.FriendManager.IsMaxFriendShipToday() then
        UISysTips.AddMsg(getLanguage("Character_Tips26"))
        return
    end]]
    local send_friendIds = {};
    local receive_friendIds = {};
    local tab_myFriend = logicMgr.FriendManager.GetMyFriendList()
    for k, v in pairs(tab_myFriend) do
        local player = v;
        if player["getLoveState"] == EnumConst.LoveStateEnum.NORMAL then
            table.insert(receive_friendIds, player["partnerId"]);
        end
        if player["sendLoveState"] == EnumConst.LoveStateEnum.NORMAL then
            table.insert(send_friendIds, player["partnerId"]);
        end
    end
    if #receive_friendIds == 0 and #send_friendIds == 0 then
        UISysTips.AddMsg("无好友可赠领")
        return
    end
    --UISysTips.AddMsg("友情点赠领成功")
    if #send_friendIds > 0 then
        friendHandler.sendGiveFriendship(send_friendIds);
    end
    if logicMgr.FriendManager.IsMaxFriendShipToday() and #send_friendIds == 0 then
        UISysTips.AddMsg("今日领取已达上限")--改成language
        return
    end
    if #receive_friendIds > 0 and not logicMgr.FriendManager.IsMaxFriendShipToday() then
        friendHandler.sendReceiveFriendship(receive_friendIds);
    end


end

function UIFriend.onClickAllRefuse()
    local tab_applyFriend = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.applyFriend)
    for k, v in pairs(tab_applyFriend) do
        friendHandler.sendRefuseFriend(v.partnerId);
    end
end

function UIFriend.onClickAllAgree()
    local tab_applyFriend = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.applyFriend)
    for k, v in pairs(tab_applyFriend) do
        friendHandler.sendAgreeFriend(v.partnerId);
    end
end

function UIFriend.onClickFreash()
    friendHandler.getFriendRecommend();
end
local last_click_time = nil
function UIFriend.onClickAllJoin()
    local s = HelperFunc.GetServerTime(false)
    if last_click_time then
        if s - last_click_time <= 500 then
            UISysTips.AddMsg(getLanguage("Sys_Friend_Tips2"))--
            return
        end
    end
    last_click_time = s

    if logicMgr.FriendManager.IsMaxFriend() then
        UISysTips.AddMsg(getLanguage("ErrorCode_140001"))
        return
    end
    local tab_applyFriend = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.recommend)
    if table.getn(tab_applyFriend) == 0 then
        UISysTips.AddMsg(getLanguage("Sys_Friend_Tips1"))
        return
    end
    for k, v in pairs(tab_applyFriend) do
        friendHandler.sendAddFriend(v.partnerId);
    end
    this.onClickFreash()
end

function UIFriend.onClickFind()
    local str = inputField.text
    if string.len(str) <= 0 then
        UISysTips.AddMsg("请输入正确角色名");
        return ;
    end
    local id = tonumber(str)

    this.isRecommendSecond = true
    if id ~= nil then
        if logicMgr.FriendManager.IsFriend(str) then
            UISysTips.AddMsg(getLanguage("ErrorCode_140000"))--已是好友
            return
        end
        friendHandler.findFriendByID(inputField.text);
    else
        if logicMgr.FriendManager.IsFriendByName(str) then
            UISysTips.AddMsg(getLanguage("ErrorCode_140000"))--已是好友
            return
        end
        friendHandler.findFriendByName(inputField.text);
    end
end

function UIFriend.updateMyFriend(isTop)
    if curState ~= State.friends then
        return ;
    end
    this.CheckShowBtnState()
    this.updateLove()
    tablist = {}
    local tab_myFriend = logicMgr.FriendManager.GetMyFriendList()
    if tab_myFriend ~= nil then
        for k, v in pairs(tab_myFriend) do
            local friend = clone(v)
            if isDelModel then
                friend.state = 4;
            else
                friend.state = 1;
            end
            table.insert(tablist, friend);
        end 
    end
    
    if isDelModel then
        table.sort(tablist, function(a, b)
            if toNumber(a.offlineTime) == 0 or toNumber(b.offlineTime) == 0 then
                return a.offlineTime > b.offlineTime
            end
            return toNumber(a.offlineTime) < toNumber(b.offlineTime)
        end)
        dynamicList_Del.itemRenderer = this.setData;
        dynamicList_Del:SetVirtual()
        dynamicList_Del.numItems = #tablist;
        HelperFunc.ListTranstionPlay(dynamicList_Del)
        if #tablist > 0 and isTop then
            dynamicList_Del:ScrollToView(0, false, true)
        end
    else
        table.sort(tablist, function(a, b)
            if toNumber(a.offlineTime) == 0 or toNumber(b.offlineTime) == 0 then
                return a.offlineTime < b.offlineTime
            end
            return toNumber(a.offlineTime) > toNumber(b.offlineTime)
        end)
        dynamicList.itemRenderer = this.setData;
        dynamicList:SetVirtual()
        dynamicList.numItems = #tablist;
        HelperFunc.ListTranstionPlay(dynamicList)
        if #tablist > 0 and isTop then
            dynamicList:ScrollToView(0, false, true)
        end
    end
    --printError(#tablist)
    txtTitle.text = getLanguage("Sys_Friend_Num") .. "(" .. #tablist .. "/" .. max_Friend .. ")";
    this.ctrlNoFriend.selectedIndex = #tablist > 0 and 0 or 1
end

function UIFriend.updateApplyFriend()
    if curState ~= State.apply then
        return ;
    end
    tablist = {}
    local tab_applyFriend = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.applyFriend)
    if tab_applyFriend ~= nil then
        for k, v in pairs(tab_applyFriend) do
            local player = clone(v)
            player.state = 2
            table.insert(tablist, player);
        end
    end
    dynamicList_Apply.itemRenderer = this.setData;
    dynamicList_Apply:SetVirtual()
    dynamicList_Apply.numItems = #tablist;
    HelperFunc.ListTranstionPlay(dynamicList_Apply)

    if #tablist > 0 then
        dynamicList_Apply:ScrollToView(0, false, true)
        this.ctrlNoFriend.selectedIndex = 0
    else
        this.ctrlNoFriend.selectedIndex = 1
    end

end

function UIFriend.updateRecommend()
    if curState ~= State.add then
        return ;
    end
    tablist = {}
    local tab_recommend = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.recommend)
    if tab_recommend ~= nil then
        for k, v in pairs(tab_recommend) do
            local player = clone(v)
            player.state = 3;
            table.insert(tablist, player);
        end
    end
    dynamicList_Add.itemRenderer = this.setData;
    dynamicList_Add:SetVirtual()
    dynamicList_Add.numItems = #tablist;
    HelperFunc.ListTranstionPlay(dynamicList_Add)
    if #tablist > 0 then
        dynamicList_Add:ScrollToView(0, false, true)
    end
    this.InitRecommendNet()
    if not this.isRecommendSecond then
        return
    end
    local friend = logicMgr.FriendManager.GetFindFriend()
    local reCommandCount = table.getn(logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.recommend))
    if not friend then
        if reCommandCount == 0 then
            this.ctrlResultOfSearch.selectedIndex = 3
        else
            this.ctrlResultOfSearch.selectedIndex = 1
        end
        return
    end
    friend.state = 3
    this.ctrlResultOfSearch.selectedIndex = 2
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.FriendInfoItem);
    if ctrlItem ~= nil then
        --初始装备信息
        ctrlItem.initData(this.findFriendItem, friend);
    end
end
function UIFriend.InitRecommendNet()
    for k, v in pairs(this.tablist) do
        local item = this.recommendNetItemList[k]
        if not item then
            break
        end
        item.obj.visible = true
        item.head.url = logicMgr.SkinManager.GetHeroIconBySkinId(v.pic)
        item.name.text = v.name
        item.title.text = v.lbs
        item.obj.onClick:Clear()
        item.obj.onClick:Add(function()
            this.ShowDetail(true, v)
        end)
    end
    for k = #this.tablist + 1, #this.recommendNetItemList do
        this.recommendNetItemList[k].obj.visible = false
    end
end
function UIFriend.ShowDetail(isShow, friend)
    if isShow then
        this.ctrlDetail.selectedIndex = 1
        this.showDetailInfo:Refresh(friend)
    else
        this.ctrlDetail.selectedIndex = 0
    end
end
function UIFriend.updateLove()
    local point = logicMgr.FriendManager.GetTodayFriendShip();
    if point > max_Love then
        point = max_Love
    end
    txtLovePoint.text = "(" .. point .. "/" .. max_Love .. ")"
    this.totalLovePoint.text = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.LOVEMONEY)
end
function UIFriend.FriendPageChanged()
    local index = friendController.selectedIndex
    if index ~= 5 then
        closeUI(PanelNames.UIFriendGift)
    end

    if index == 6 then
        txtTitle.text = string.format(getLanguage("UIBlackList"), "(0/" .. logicMgr.FriendManager.GetBlackMax() .. ")")
        if m_blackInit then
            UIFriend.RefreshBlackList()
        else
            friendHandler.ReqBlackList()
        end
    end
end
function UIFriend.FreshRoleInfoOtherMsg(info)
    this.isOpenShow = true
    showUI(PanelNames.UIPlayerShow, info,nil,false, function()
        this.isOpenShow = false;
    end,false,function ()
        UIBottom.onSetBattle();
        showUI(PanelNames.UIFriend);
    end)
end

function UIFriend.ShowAddMessage(isShow, friendId)
    if isShow then
        this.ctrlAddMessage.selectedIndex = 1
        this.addMessagePanel:Refresh(friendId)
    else
        this.ctrlAddMessage.selectedIndex = 0
    end

end
--打开好友切磋
function UIFriend.OpenFriendCompeteBattle(msg)
    --if this.isOpenShow then
    --    return ;
    --end
    if isOpenUI(PanelNames.UIPlayerShow) then
        return ;
    end
    local param = {};
    --param.playerId = 0;
    local info = logicMgr.FriendManager.GetFriendCompeteBattleInfo()
    param.power = 0;
    param.supSkillId = info.supportSkill;
    param.wins = info.winTimes;
    param.competeTimes = info.competeTimes;
    param.isFriend = FriendManager.IsFriend(msg.friendId) and true or false;
    --param.type = EnumConst.FormationType.ArenaDef;
    logicMgr.FriendManager.SetFriendCompeteBattleType(EnumConst.FightTypeConst.FriendCompeteBattle)

    showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.FriendCompeteBattle, param, function(formations)

        local flag = true;
        if formations[1] ~= nil then
            for i = 1, #formations[1] do
                if formations[1][i] ~= nil and tonumber(tostring(formations[1][i])) > 0 then
                    flag = false;
                    break ;
                end
            end
        end
        if flag then
            UISysTips.AddMsg("需要上阵至少一名英雄");
        else
            local supSkills = logicMgr.SkillManager.getSupSkillInfo();
            local supSkillId = 0;
            for i = 1, #supSkills do
                if supSkills[i].isUse == 1 then
                    supSkillId = tostring(supSkills[i].skillId);
                end
            end
            local info = logicMgr.FriendManager.GetFriendCompeteBattleInfo()

            friendHandler.ReqFriendCompeteBattle(info.friendId, formations[1], supSkillId)
        end
    end, function()
        --showCtrl.selectedPage = "show";
    end)

end
--打开好友切磋记录
function UIFriend.OpenFriendCompeteBattleRecord()
    showUI(PanelNames.UIFriendCommunication)
end

function UIFriend.blackItemRender(index, obj)
    local data = m_blackList[index + 1]

    local btn_head = obj:GetChild("Head")
    local playerName = obj:GetChild("Name")
    local txt_level = obj:GetChild("Level")
    local txt_province = obj:GetChild("province")
    local icon = btn_head:GetChild("head"):GetChild("HeadIcon")
    local frame = btn_head:GetChild("frame")
    local btn_Remove = obj:GetChild("btn_Remove")

    btn_Remove.onClick:Clear()
    btn_Remove.onClick:Add(function()
        friendHandler.ReqBlackPLayer(tostring(data.playerId), 2)
    end)

    icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(data.pic)
    playerName.text = data.name
    -- item.title.text = v.lbs
    txt_level.text = data.level
    frame.url =  "ui://ICONAtlas/" .. data.picFrame

    -- self:SetPlayerIcon(icon, serverData.playerInfo.headIcon, frame, serverData.playerInfo.headFrame)
end

function UIFriend.RefreshBlackList()
    m_blackInit = true
    m_blackList = logicMgr.FriendManager.GetBlackList()
    txtTitle.text = string.format(getLanguage("UIBlackList"), "(" .. #m_blackList .. "/" .. logicMgr.FriendManager.GetBlackMax() .. ")")
    if m_blackList then
        this.scroll_black.numItems = #m_blackList
    else

    end
end

function UIFriend.OnBlackListRefresh()
    m_blackList = logicMgr.FriendManager.GetBlackList()
    txtTitle.text = string.format(getLanguage("UIBlackList"), "(" .. #m_blackList .. "/" .. logicMgr.FriendManager.GetBlackMax() .. ")")
    this.scroll_black.numItems = #m_blackList
end

return UIFriend;
